package com.example.vocab.controller;

import com.alibaba.fastjson.JSON;
import com.example.vocab.dao.UserDao;
import com.example.vocab.entity.User;
import com.example.vocab.service.UserService;
import com.example.vocab.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;

@RestController
public class LoginController {

//    @Autowired
//    @Qualifier("userServiceImpl")
    @Resource(name="userServiceImpl")
    UserService userService;
//    @Autowired
//    UserDao userDao;

    @RequestMapping("tologin")
    public String login(@RequestBody User user)
    {
        User res = userService.getUser(user);
        HashMap<String,Object> resMap=new HashMap<>();
        if(res.getUsername()==null)
        {
            resMap.put("flag","fail");
        }
        else
        {
            resMap.put("flag","success");
            resMap.put("user",res);
        }
        String resJson=JSON.toJSONString(resMap);
        return resJson;
    }
}
